package com.batchengine.system;

import static com.batchengine.util.DateUtil.formatSecsToHHMMSS;
import static com.batchengine.util.DateUtil.parseDate;
//import sun.management.ManagementFactory;

import java.lang.management.*;
import java.util.Date;

/**
 * Created by sunil.incubator@yahoo.com.
 * User: sunil.kalva
 * Date: Aug 13, 2009
 */
public class SystemMemoryInfo {

    private Long heapUsed;
    private Long heapMax;

    private Long nonHeapUsed;
    private Long nonHeapMax;

    private Integer totalThreads;

    private String osName;
    private Long upTime;
    private Date startTime;

    public String getUpTimeStr() {
        return formatSecsToHHMMSS(upTime);
    }

    public Long getUpTime() {
        return upTime;
    }

    public void setUpTime(Long upTime) {
        this.upTime = upTime;
    }

    public Long getHeapUsed() {
        return heapUsed;
    }

    public void setHeapUsed(Long heapUsed) {
        this.heapUsed = heapUsed;
    }

    public Long getHeapMax() {
        return heapMax;
    }

    public void setHeapMax(Long heapMax) {
        this.heapMax = heapMax;
    }

    public Long getNonHeapUsed() {
        return nonHeapUsed;
    }

    public void setNonHeapUsed(Long nonHeapUsed) {
        this.nonHeapUsed = nonHeapUsed;
    }

    public Long getNonHeapMax() {
        return nonHeapMax;
    }

    public void setNonHeapMax(Long nonHeapMax) {
        this.nonHeapMax = nonHeapMax;
    }

    public Integer getTotalThreads() {
        return totalThreads;
    }

    public void setTotalThreads(Integer totalThreads) {
        this.totalThreads = totalThreads;
    }

    public String getOsName() {
        return osName;
    }

    public void setOsName(String osName) {
        this.osName = osName;
    }

    public Date getStartTime() {
        return startTime;
    }

    public String getStartTimeStr() {
        return parseDate(startTime);
    }

    public void setStartTime(Date startTime) {
        this.startTime = startTime;
    }

    public static SystemMemoryInfo getSystemMemoryInfo() {

        SystemMemoryInfo memoryInfo = new SystemMemoryInfo();

        MemoryMXBean bean = ManagementFactory.getMemoryMXBean();
        memoryInfo.setHeapMax(bean.getHeapMemoryUsage().getMax() / 1024);
        memoryInfo.setHeapUsed(bean.getHeapMemoryUsage().getUsed() / 1024);
        memoryInfo.setNonHeapMax(bean.getNonHeapMemoryUsage().getMax() / 1024);
        memoryInfo.setNonHeapUsed(bean.getNonHeapMemoryUsage().getUsed() / 1024);

        OperatingSystemMXBean operatingSystemMXBean = ManagementFactory.getOperatingSystemMXBean();
        memoryInfo.setOsName(operatingSystemMXBean.getName());

        RuntimeMXBean runtimeMXBean = ManagementFactory.getRuntimeMXBean();
        memoryInfo.setUpTime(runtimeMXBean.getUptime() / 1000);
        memoryInfo.setStartTime(new Date(runtimeMXBean.getStartTime()));

        ThreadMXBean threadMXBean = ManagementFactory.getThreadMXBean();
        memoryInfo.setTotalThreads(threadMXBean.getThreadCount());

        return memoryInfo;
    }

    @Override
    public String toString() {
        return "SystemMemoryInfo{" +
                "heapUsed=" + heapUsed +
                ", heapMax=" + heapMax +
                ", nonHeapUsed=" + nonHeapUsed +
                ", nonHeapMax=" + nonHeapMax +
                ", totalThreads=" + totalThreads +
                ", osName='" + osName + '\'' +
                ", upTime=" + upTime +
                ", startTime=" + startTime +
                '}';
    }

    public static void main(String[] args) {

        System.out.println(SystemMemoryInfo.getSystemMemoryInfo());
    }
}
